package uniandes.dermatologia;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import uniandes.dermatologia.resources.Base64;
import uniandes.dermatologia.singleton.DermatologiaUniandesApp;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

public class PhotoActivity extends Activity {

	/**
	 * File that is going to be uploaded
	 */
	private File archivoASubir;
	/**
	 * Last file attached
	 */
	private String lastAttached;
	/**
	 * Prefix for all the picture attachments
	 */
	private static final String JPEG_FILE_PREFIX = "IMG_";

	/**
	 * Suffix for all the picture attachments
	 */
	private static final String JPEG_FILE_SUFFIX = ".jpg";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);

		getActionBar().setDisplayHomeAsUpEnabled(false);
	}

	/**
	 * Displays the photo camera
	 * 
	 * @param v
	 *            View where is called
	 */
	public void takePhoto(View v) {
		try {
			dispatchTakePictureIntent(0);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void pickPhoto(View v) throws IOException {
		Intent pickPhoto = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);		
		startActivityForResult(pickPhoto, 1);

	}

	private void dispatchTakePictureIntent(int actionCode) throws IOException {
		Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File rootsd = Environment
				.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

		File sdImageMainDirectory = new File(rootsd.getAbsolutePath()
				+ "/Dermatologia/data");

		if (!sdImageMainDirectory.exists()) {
			sdImageMainDirectory.mkdirs();
		}
		archivoASubir = createImageFile();
		takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
				Uri.fromFile(archivoASubir));

		startActivityForResult(takePictureIntent, actionCode);
	}

	private File createImageFile() throws IOException {
		File rootsd = Environment
				.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
		File albumF = new File(rootsd.getAbsolutePath() + "/Dermatologia/data");
		File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX,
				albumF);
		lastAttached = imageFileName;
		return imageF;
	}

	public String encodeImage(byte[] image) throws IOException {
		String resultado = Base64.encodeBytes(image);
		return resultado;
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {

				String base64Attached;

				try {
					Bitmap bm = BitmapFactory.decodeFile(archivoASubir
							.getAbsolutePath());
					ByteArrayOutputStream stream = new ByteArrayOutputStream();
					bm.compress(Bitmap.CompressFormat.JPEG, 60, stream);
					byte[] byteArray = stream.toByteArray();

					base64Attached = encodeImage(byteArray);

					((DermatologiaUniandesApp) getApplication())
							.getCurrentConsultation().setPhoto(base64Attached);

					View b = findViewById(R.id.btnTakePhoto);
					b.setVisibility(View.GONE);

				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				
			}
		}
	}

	public void onClickNext(View v) {

		Intent intent = new Intent(this, SendActivity.class);
		startActivity(intent);
	}

	public void onClickBack(View v) {
		finish();
	}

}
